#!/usr/bin/env python3
"""
系统设置标签页组件

实现系统配置管理界面，包括：
- 常规设置
- 编码设置
- 硬件设置
"""

from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, 
                             QComboBox, QSpinBox, QDoubleSpinBox, QCheckBox,
                             QLineEdit, QPushButton, QGroupBox, QFormLayout,
                             QTabWidget, QScrollArea)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont


class GeneralSettingsGroup(QGroupBox):
    """常规设置组"""
    
    def __init__(self, parent=None):
        super().__init__("⚙️ 常规设置", parent)
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        layout = QFormLayout(self)
        
        # 输出目录
        self.output_dir_edit = QLineEdit()
        self.browse_btn = QPushButton("浏览...")
        
        dir_layout = QHBoxLayout()
        dir_layout.addWidget(self.output_dir_edit)
        dir_layout.addWidget(self.browse_btn)
        
        layout.addRow("输出目录:", dir_layout)
        
        # 并发任务数
        self.max_concurrent_spin = QSpinBox()
        self.max_concurrent_spin.setRange(1, 16)
        self.max_concurrent_spin.setValue(4)
        layout.addRow("最大并发任务数:", self.max_concurrent_spin)
        
        # 完成后操作
        self.after_completion_combo = QComboBox()
        self.after_completion_combo.addItems([
            "无操作",
            "关机",
            "休眠",
            "退出程序"
        ])
        layout.addRow("完成后操作:", self.after_completion_combo)
        
        # 自动保存设置
        self.auto_save_check = QCheckBox("自动保存设置")
        self.auto_save_check.setChecked(True)
        layout.addRow("", self.auto_save_check)


class EncoderSettingsGroup(QGroupBox):
    """编码设置组"""
    
    def __init__(self, parent=None):
        super().__init__("🎬 编码设置", parent)
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        layout = QFormLayout(self)
        
        # 视频编码器
        self.video_encoder_combo = QComboBox()
        self.video_encoder_combo.addItems([
            "H.264",
            "H.265 (HEVC)",
            "AV1",
            "VP9"
        ])
        layout.addRow("视频编码器:", self.video_encoder_combo)
        
        # 音频编码器
        self.audio_encoder_combo = QComboBox()
        self.audio_encoder_combo.addItems([
            "AAC",
            "MP3",
            "AC3",
            "Opus"
        ])
        layout.addRow("音频编码器:", self.audio_encoder_combo)
        
        # 视频质量
        self.video_quality_combo = QComboBox()
        self.video_quality_combo.addItems([
            "高质量（文件较大）",
            "平衡",
            "快速（文件较小）"
        ])
        layout.addRow("视频质量:", self.video_quality_combo)
        
        # 目标比特率
        self.bitrate_spin = QSpinBox()
        self.bitrate_spin.setRange(100, 50000)
        self.bitrate_spin.setValue(4000)
        self.bitrate_spin.setSuffix(" kbps")
        layout.addRow("目标比特率:", self.bitrate_spin)
        
        # 帧率
        self.fps_spin = QDoubleSpinBox()
        self.fps_spin.setRange(1, 120)
        self.fps_spin.setValue(30)
        self.fps_spin.setSuffix(" fps")
        layout.addRow("帧率:", self.fps_spin)


class HardwareSettingsGroup(QGroupBox):
    """硬件设置组"""
    
    def __init__(self, parent=None):
        super().__init__("💻 硬件设置", parent)
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        layout = QFormLayout(self)
        
        # GPU加速
        self.gpu_acceleration_check = QCheckBox("启用GPU加速")
        self.gpu_acceleration_check.setChecked(True)
        layout.addRow("", self.gpu_acceleration_check)
        
        # 首选GPU
        self.preferred_gpu_combo = QComboBox()
        self.preferred_gpu_combo.addItems([
            "自动选择",
            "独显优先",
            "集显优先",
            "仅使用CPU"
        ])
        layout.addRow("首选GPU:", self.preferred_gpu_combo)
        
        # CPU线程数
        self.cpu_threads_spin = QSpinBox()
        self.cpu_threads_spin.setRange(1, 32)
        self.cpu_threads_spin.setValue(0)
        self.cpu_threads_spin.setSpecialValueText("自动")
        layout.addRow("CPU线程数:", self.cpu_threads_spin)
        
        # 内存限制
        self.memory_limit_spin = QSpinBox()
        self.memory_limit_spin.setRange(512, 32768)
        self.memory_limit_spin.setValue(4096)
        self.memory_limit_spin.setSuffix(" MB")
        layout.addRow("内存限制:", self.memory_limit_spin)
        
        # 温度保护
        self.temp_protection_check = QCheckBox("启用温度保护")
        self.temp_protection_check.setChecked(True)
        layout.addRow("", self.temp_protection_check)
        
        # 最高温度
        self.max_temp_spin = QSpinBox()
        self.max_temp_spin.setRange(60, 95)
        self.max_temp_spin.setValue(85)
        self.max_temp_spin.setSuffix(" °C")
        layout.addRow("最高温度:", self.max_temp_spin)


class AdvancedSettingsGroup(QGroupBox):
    """高级设置组"""
    
    def __init__(self, parent=None):
        super().__init__("🔧 高级设置", parent)
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        layout = QFormLayout(self)
        
        # FFmpeg参数
        self.ffmpeg_args_edit = QLineEdit()
        self.ffmpeg_args_edit.setPlaceholderText("额外的FFmpeg参数")
        layout.addRow("FFmpeg参数:", self.ffmpeg_args_edit)
        
        # 日志级别
        self.log_level_combo = QComboBox()
        self.log_level_combo.addItems([
            "静默",
            "错误",
            "警告",
            "信息",
            "调试"
        ])
        self.log_level_combo.setCurrentText("信息")
        layout.addRow("日志级别:", self.log_level_combo)
        
        # 临时文件目录
        self.temp_dir_edit = QLineEdit()
        self.temp_browse_btn = QPushButton("浏览...")
        
        temp_layout = QHBoxLayout()
        temp_layout.addWidget(self.temp_dir_edit)
        temp_layout.addWidget(self.temp_browse_btn)
        
        layout.addRow("临时文件目录:", temp_layout)
        
        # 保留临时文件
        self.keep_temp_check = QCheckBox("保留临时文件")
        self.keep_temp_check.setChecked(False)
        layout.addRow("", self.keep_temp_check)


class SettingsTab(QWidget):
    """系统设置标签页"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
    
    def init_ui(self):
        """初始化界面"""
        # 创建主布局
        layout = QVBoxLayout(self)
        
        # 标题
        title_label = QLabel("⚙️ 系统设置")
        title_label.setFont(QFont("Arial", 16, QFont.Weight.Bold))
        title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        layout.addWidget(title_label)
        
        # 常规设置
        self.general_group = GeneralSettingsGroup()
        layout.addWidget(self.general_group)
        
        # 编码设置
        self.encoder_group = EncoderSettingsGroup()
        layout.addWidget(self.encoder_group)
        
        # 硬件设置
        self.hardware_group = HardwareSettingsGroup()
        layout.addWidget(self.hardware_group)
        
        # 高级设置
        self.advanced_group = AdvancedSettingsGroup()
        layout.addWidget(self.advanced_group)
        
        # 按钮区域
        button_layout = QHBoxLayout()
        
        self.save_btn = QPushButton("💾 保存设置")
        self.reset_btn = QPushButton("🔄 恢复默认")
        self.apply_btn = QPushButton("✅ 应用")
        
        button_layout.addWidget(self.save_btn)
        button_layout.addWidget(self.reset_btn)
        button_layout.addWidget(self.apply_btn)
        button_layout.addStretch()
        
        layout.addLayout(button_layout)
        
        # 添加弹性空间，允许窗口调整高度
        layout.addStretch()
    
    def load_settings(self):
        """加载设置"""
        # 这里应该从配置文件加载设置
        pass
    
    def save_settings(self):
        """保存设置"""
        # 这里应该保存设置到配置文件
        pass
    
    def reset_to_default(self):
        """恢复默认设置"""
        # 这里应该重置所有设置为默认值
        pass